using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections;
using System.Text;
using System.IO;
using System.Configuration;
using System.Reflection;

namespace InsightSPWS
{
    /// <summary>
    /// Summary description for InsightSPWS
    /// </summary>
    public class InsightSPWS : System.Web.Services.WebService
    {
        
        /// <summary>
        /// Moves the content of one folder to other folder and deletes original folder
        /// </summary>
        /// <param name="originalFolderName">"Shared Documents/Humans"</param>
        /// <param name="newFolderName">"Shared Documents/Humans_2"</param>
        [WebMethod]
        public void RenameFolder(string siteCollectionUrl, string originalFolderName, string newFolderName)
        {
            //Move File and delete the folder
            try
            {
              
                using (SPSite site = new SPSite(siteCollectionUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPFolder spFolder = web.GetFolder(originalFolderName);
                        web.AllowUnsafeUpdates = true;
                        spFolder.MoveTo(siteCollectionUrl + "/"+newFolderName);

                    }
                }
              
            }
            catch (Exception ex)
            {
               
                throw ex;
            }

        }
        /// <summary>
        /// Creates a link in sharepoint to the doucment in different folder
        /// </summary>
        /// <param name="folderURl">Shared Documents/Humans</param>
        /// <param name="fileLinkUrl">Shared Documents/Animals/1.docx</param>
        /// <param name="fileName">1.aspx</param>
        [WebMethod]
        public void CreateLinkToDocument(string siteCollectionUrl , string folderURl, string fileLinkUrl, string fileName)
        {

            try
            {
                InsightSPWS ap = this;
                using (SPSite siteCollection = new SPSite(siteCollectionUrl))
                {
                    using (SPWeb web = siteCollection.OpenWeb())
                    {
                        SPFolder list = web.GetFolder(folderURl) ;
                        //link to the file
                        StringBuilder builder = new StringBuilder();
                        //This file contains microsoft specified schema and standards to create link;
                        using (TextReader reader = new StreamReader(@"C:\LinkToDocumentTemplate.txt"))
                        {
                            builder.Append(reader.ReadToEnd());
                        }
                        //replace string template with values
                        builder.Replace("{0}", fileLinkUrl);
                        //should change the name of the .aspx file per item
                        SPFile file = list.Files.Add(fileName, UTF8Encoding.UTF8.GetBytes(builder.ToString()),true);
                        //set list item properties
                        SPListItem item = file.Item;
                        item["Content Type"] = "Link to a Document";
                        SPFieldUrlValue itemUrl = new SPFieldUrlValue();
                        itemUrl.Description = "From Document Library";
                        itemUrl.Url = siteCollectionUrl+"/"+fileLinkUrl;
                        item["URL"] = itemUrl;
                        //persist changes
                        item.Update();
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }

}

